# 1.用for循环和while来完成简单的计数
def my_for_calculation():
    my_result = 0
    for i in range(10):
        my_result += i
    print(my_result)


def my_while_calculation():
    n = 0
    my_result = 0
    while n < 10:
        my_result += n
        n = n + 1
    print(my_result)


# - 2.实现斐波那契函数(for循环, while循环)
# - 如果遇到可以转换为`int`类型的参数, 则转换, 否则抛出自定义异常`ParamsTypeError`
class ParamsTypeError(Exception):
    pass


def fib_for(x):
    a, b = 0, 1
    c = 0
    my_r = [a, b]
    try:
        x = int(x)
        if x == 0:
            return [a]
        elif x == 1:
            return [a, b]
        for i in range(x-1):
            c = a
            a = a + b
            b = c
            if i > 0:
                my_r.append(a)
        return my_r
    except Exception as e:
        raise ParamsTypeError("类型不对,请输入正确的类型")


def fib_while(x):
    a, b = 0, 1
    c = 0
    my_r = [a, b]
    try:
        x = int(x)
        if x == 0:
            return [a]
        elif x == 1:
            return [a, b]
        i = 0
        while i < x-1:
            c = a
            a = a + b
            b = c
            if i > 0:
                my_r.append(a)
            i += 1
        return my_r
    except Exception as e:
        raise ParamsTypeError("类型不对,请输入正确的类型")


#  在第二周-第一节课我们实现的简单计算器的基础上, 对参数进行检查, 如果报错就抛出我们自定义异常`ParamsError`
#  已在第二周-第一节课的作业my_calculator.py进行更改.


if __name__ == "__main__":
    my_for_calculation()
    my_while_calculation()
    print("斐波那契函数:请输入100以内的整数!:\n")
    my_input = input()
    print(f"斐波那契函数:{fib_for(my_input)}")
    print(f"斐波那契函数:{fib_while(my_input)}")
